// reference_counter.h
#ifndef REFERENCE_COUNTER_H
#define REFERENCE_COUNTER_H

#include <cstddef>

/**
 * ReferenceCounter is used as base class for MatrixImplementation to implement reference counting.
 * Not much special about it, it has a virtual destructor and is in it self responsible for deletion when the counter reaches 0.
 * A thing worth noting is that the reference counter is not thread safe, but then again nothing presented here is thread safe.
 * 
 * I have cosen to implement my own reference counter over using existing solutions (shared_ptr) to gain some experience in 
 * have to make copy-on-write on a lower level.
 */
class ReferenceCounter
{
private:
    size_t _refCounter;

public:
    ReferenceCounter() : _refCounter(1) { }
    ReferenceCounter(ReferenceCounter const & other) : _refCounter(1) { }
    virtual ~ReferenceCounter() { }

    inline void AddRef() { ++_refCounter; }
    inline void RemoveRef() { if(--_refCounter == 0) delete this; }
    inline bool UniqueRef() { return _refCounter == 1;}
};


#endif // REFERENCE_COUNTER_H
